package com.slkj.shilixiaoyuanapp.ui.teacher.tool.MyApproval;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.entity.ToolPurchaseDetailEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.common.ChosePeopleActivity;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.Purchase.PurchasePassedActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.slkj.shilixiaoyuanapp.view.NumberEditText;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;
import com.tkk.api.RxEventProcessor;

import butterknife.BindView;
import butterknife.OnClick;

@ActivityInfo(layout = R.layout.activity_sp_purchase, title = "采购 ")
public class SpPurchaseActivity extends BaseActivity {
    @InjectParam
    int id;
    @InjectParam
    ToolPurchaseDetailEntity data;
    @InjectParam
    int aId;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_menoy)
    TextView tvMenoy;
    @BindView(R.id.tv_now_day_time)
    TextView tvNowDayTime;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.layout_chose_type)
    RelativeLayout layoutChoseType;
    @BindView(R.id.edit_num)
    EditText editNum;
    @BindView(R.id.edit_price)
    EditText editPrice;
    @BindView(R.id.tv_bm)
    TextView tvBm;
    @BindView(R.id.layoout_bumen)
    RelativeLayout layooutBumen;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.layout_chose_time)
    RelativeLayout layoutChoseTime;
    @BindView(R.id.edit_content)
    NumberEditText editContent;
    @BindView(R.id.recycer_spr)
    RecyclerView recycerSpr;
    SqrAdapter sqrAdapter;
    @BindView(R.id.edit_reson)
    EditText editReson;
    @BindView(R.id.layout_botton)
    View viewBotton;
    @Override
    protected void init() {
        SpPurchaseActivity__JumpCenter.bind(this);
        RxEventProcessor.get().bind(this);
        recycerSpr.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.HORIZONTAL));
        recycerSpr.setLayoutManager(new LinearLayoutManager(this));
        if (data.getAuditor() != null && !data.getAuditor().isEmpty()){
            sqrAdapter = new SqrAdapter(data.getAuditor());
            recycerSpr.setAdapter(sqrAdapter);
        }
        tvUserName.setText(data.getName());
        tvNowDayTime.setText(data.getIntime());
        editName.setText(data.getGoods());
        editContent.setText(data.getBody());
        tvBm.setText(data.getDepartment().getdName());
        editContent.setText(data.getBody());
        editNum.setText(String.valueOf(data.getNum()));
        editPrice.setText(String.valueOf(data.getMoney()));
        tvTime.setText(data.getHoursOfUse());
        try{
            int num = Integer.valueOf(editNum.getText().toString());
            double price = Double.valueOf(editPrice.getText().toString());
            tvMenoy.setText(String.valueOf(num*price));
        }catch (Exception e){
            tvMenoy.setText(String.valueOf(""));

        }
        viewBotton.setVisibility(data.getMyState() == MyApprovalHistoryFragment.TYPE_NOT_LOOK?View.VISIBLE:View.GONE);
        editReson.setFocusable(data.getMyState()==MyApprovalHistoryFragment.TYPE_NOT_LOOK?true:false);
        editReson.setText(data.getRefute());
    }
    @OnClick(R.id.tv_tg)void tg(){
        String reson = editReson.getText().toString();
        upData(reson,2);

    }

    private void upData(String reson,int state) {
        HttpHeper.get().toolService()
                .spDo(id,aId,state,reson)
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<String>(true, this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        //showToast(data);
                        LoadSuccessAndFailDialog.showSuccess(SpPurchaseActivity.this,data);
                        finish();
                    }
                });
    }

    @OnClick(R.id.tv_wtg)void wtg(){
        String reson = editReson.getText().toString();
        if (TextUtils.isEmpty(reson)){
            showToast("请填写拒绝原因");
            return;
        }
        upData(reson,1);
    }
}
